<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>响应 JSON 数据</title>
    <style>
        span{
            font-size: 12px;
            color: rgb(168, 23, 23);
        }
    </style>
  </head>
  <body>
    手机号: <input type="text" /> <span></span>

    <script>
      //GET  http://127.0.0.1:8080/check-phone
      let input = document.querySelector("input");
      let span = document.querySelector("span");
      //绑定事件
      input.onblur = function () {
        //获取 input 中的输入值
        let v = input.value;
        //发送请求
        let xhr = new XMLHttpRequest();
        //设置响应体的类型
        // xhr.responseType = "json"; // response 响应 type 类型
        //
        xhr.open("get", "http://127.0.0.1:8080/check-phone?phone=" + v);
        //
        xhr.send();
        //
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            //方式一  手动转换
            let data = JSON.parse(xhr.response);
            //方式二  自动转换   xhr.responseType = "json";
            // console.log(xhr.response);
            //处理错误的信息
            span.innerHTML = xhr.response.msg;
          }
        };
      };
    </script>
  </body>
</html>
